﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;

namespace Personal_Assistant
{
    class PA
    {
        public List<string> CheckToday()
        {
            List<string> all = new List<string>();
            foreach (var data in Directory.GetFiles(@"PersonalAssistant\AllDates"))
            {
                if (Convert.ToDateTime(File.ReadAllText(data)) == DateTime.Today)
                {
                    all.Add(data);
                }
            }

            return all;
        }

        public void DisplayMonth(int month, int year)
        {
            int j = 1;
            DateTime d = new DateTime(year, month, 1);

            string[] dt = d.ToLongDateString().Split(',');
            string may = dt[1].Replace(dt[1].Substring(dt[1].Length - 2, 2), "") + dt[2];

            Console.WriteLine();
            Console.CursorLeft = 30;
            WriteYellowText("CALENDAR FOR" + may.ToUpper());
            Console.WriteLine("\n\nNOTE: Days in yellow indicate an appointment on that day\n");
            Console.WriteLine("\nSun \tMon \tTue \tWed \tThur \tFri \tSat");

            switch (d.DayOfWeek.ToString().ToUpper())
            {
                case "MONDAY": Console.Write("\t"); j = 2; break;
                case "TUESDAY": Console.Write("\t\t"); j = 3; break;
                case "WEDNESDAY": Console.Write("\t\t\t"); j = 4; break;
                case "THURSDAY": Console.Write("\t\t\t\t"); j = 5; break;
                case "FRIDAY": Console.Write("\t\t\t\t\t"); j = 6; break;
                case "SATURDAY": Console.Write("\t\t\t\t\t\t"); j = 7; break;
            }

            for (int i = 1; i <= DateTime.DaysInMonth(year, month); i++)
            {
                int year1 = 0; int month1 = 0; int day1 = 0;
                foreach (var data in Directory.GetFiles(@"PersonalAssistant\AllDates"))
                {
                    string date = File.ReadAllText(data);
                    year1 = int.Parse(date.Split('-')[0]);
                    month1 = int.Parse(date.Split('-')[1]);
                    day1 = int.Parse(date.Split('-')[2]);

                    if (year == year1 && month == month1 && i == day1)
                        break;
                }

                if (year == year1 && month == month1 && i == day1)
                    WriteYellowText(i.ToString());
                else
                    Console.Write(i);

                Console.Write("\t"); j++;

                if (j > 7) { Console.WriteLine(); j = 1; }
            }

            Console.WriteLine();
        }

        /* Creates the required folders for the program
        *  Refer to Lines 2100 - 2122 in "PersonalAssistant.cs" class
        */
        public void CreateReqDirs()
        {
            Directory.CreateDirectory("PersonalAssistant");
            Directory.CreateDirectory("PersonalAssistant/Contacts");
            Directory.CreateDirectory("PersonalAssistant/Appointments");
            Directory.CreateDirectory("PersonalAssistant/AllDates");
            Directory.CreateDirectory("PersonalAssistant/Calendar");
        }

        public bool IsValid(string val, int range)
        {
            bool _valid = false;
            string[] used_alp = { "A", "B", "C", "D", "E", "F", "G", "H", "I" };

            for (int i = 0; i < range; i++)
            {
                if (val == used_alp[i]) { _valid = true; break; }
                else { _valid = false; }
            }

            return _valid;
        }

        public string ReadValidComp()
        {
            string val = Console.ReadLine();
            while (val == "")
            {
                Console.WriteLine("\nFIELD CANNOT BE BLANK. PLEASE ENTER A VALUE");
                val = Console.ReadLine();
            }

            return val;
        }

        public string ReadValidGender(string st)
        {
            string val = Console.ReadLine();

            while (val.ToUpper() != "MALE" && val.ToUpper() != "FEMALE" && val != "")
            {
                Console.WriteLine("\nGENDER CAN ONLY BE MALE OR FEMALE");
                val = Console.ReadLine();
            }

            return val;
        }

        public string ReadValidDate(string spd)
        {
            string val = Console.ReadLine();
            string regexString = @"\d{4}-\d{2}-\d{2}";

            Match matches = Regex.Match(val, regexString);

            while (val == "" && spd == "DOM")
            {
                Console.WriteLine("\nFIELD CANNOT BE BLANK. PLEASE ENTER A VALUE");
                val = Console.ReadLine();
            }

            if (val != "")
            {
                while ((matches.Success == false && val != "") || (val == "" && spd == "DOM"))
                {
                    Console.WriteLine("\nENTER DATE IN FORMAT (Year-Month-Day)");
                    val = Console.ReadLine();

                    matches = Regex.Match(val, regexString);
                }

                DateTime data = DateTime.Now;
                while ((!DateTime.TryParse(val, out data) && val != "") || (val == "" && spd == "DOM"))
                {
                    Console.WriteLine("\nENTERED DATE NOT VALID. PLEASE RE-ENTER");
                    val = Console.ReadLine();
                }

                if (spd == "DOB" && val != "")
                {
                    while ((Convert.ToDateTime(val) > DateTime.Now))
                    {
                        Console.WriteLine("\nDATE OF BIRTH CANNOT BE GREATER THAN TODAY");
                        val = Console.ReadLine();

                        while (!DateTime.TryParse(val, out data) && val != "")
                        {
                            Console.WriteLine("\nENTERED DATE NOT VALID. PLEASE RE-ENTER");
                            val = Console.ReadLine();
                        }
                    }
                }
            }

            return val;
        }

        public string ReadValidTime(bool edt)
        {
            string final = Console.ReadLine();
            string regexString = @"^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$";

            Match matches = Regex.Match(final, regexString);

            while (matches.Success == false)
            {
                if (edt && final == "")
                    break;
                Console.WriteLine("\nENTER TIME IN 24hr FORMAT (HH:MM)");
                final = Console.ReadLine();

                matches = Regex.Match(final, regexString);
            }

            return final;
        }

        public string ReadValidPhNo(string st)
        {
            string val = Console.ReadLine();

            if (!(st == "EDIT" && val == ""))
            {
                string regex = @"\d";
                Match match = Regex.Match(val, regex);
                while (!match.Success)
                {
                    Console.WriteLine("\nPHONE NUMBER CAN ONLY CONTAIN NUMERIC DATA");
                    val = Console.ReadLine();
                    regex = @"\d";
                    match = Regex.Match(val, regex);
                    if (match.Success)
                    {
                    }
                }
            }

            return val;
        }

        public void PreviewOutput(Dictionary<string, string> data)
        {
            foreach (var dt in data)
            {
                WriteYellowText(dt.Key + ":");
                Console.Write("\t" + dt.Value + "\n");
            }
        }

        public void ExitApp()
        {
            Console.WriteLine("\n*******************************************\nThank you for using Personal Assistant \nHope to see you again soon.");
        }

        public void WriteYellowText(string text)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(text);
            Console.ForegroundColor = ConsoleColor.White;
        }

        public void WriteRedText(string text)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(text);
            Console.ForegroundColor = ConsoleColor.White;
        }
    }
}
